//
//  MainViewController.swift
//  StealPicture
//
//  Created by ?! 郭斌勇 on 16/8/10.
//  Copyright © 2016年 ?! 郭斌勇. All rights reserved.
//

import UIKit

let Key_引导页已显示 = "Key_引导页已显示"

class MainViewController: UIViewController,UIScrollViewDelegate {
    
    var  内容viewController:UIViewController?
    var 引导页viewController:UIViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        // Do any additional setup after loading the view.
        
        let _ = 用途类别集合_全局量   //此句的目的在于让他开始加载数据
        
        内容viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController")
        self.addChildViewController(内容viewController!)
        
        内容或引导页显示决策_函数()

        
        
        
        
        
    }
    
    
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    

    
    
    
    
    //MARK:内容 或者 引导 显示决断
    
    func 内容或引导页显示决策_函数(){
        if NSUserDefaults.standardUserDefaults().boolForKey(Key_引导页已显示) {
            内容viewController?.view.frame = self.view.bounds
            self.view.addSubview((内容viewController?.view)!)
           
        }else{
            显示引导页_函数()
        }
        
        
        
    }
    
    
    func 显示引导页_函数(){
        引导页viewController = self.storyboard?.instantiateViewControllerWithIdentifier("引导页ViewController")
        self.addChildViewController(引导页viewController!)
        引导页viewController?.view.frame = self.view.bounds
        self.view.addSubview((引导页viewController?.view)!)
        let 滑动引导view = 引导页viewController?.view.subviews.first?.subviews.first as? UIScrollView
        if let 引导scrollView = 滑动引导view{
            引导scrollView.delegate = self
        }else{
            print("\(self)没有找到引导scrollView")
        }
        
    }
    
    
    func 从引导页切换到内容_函数(){
        
        self.transitionFromViewController(引导页viewController!, toViewController: 内容viewController!, duration: 1, options: .AllowUserInteraction, animations: nil, completion: {if $0 {
            self.存储引导页显示状态()
            self.引导页viewController?.removeFromParentViewController()
            self.引导页viewController = nil 
            }
        })
        
        
    }
    
    
    func 存储引导页显示状态(){
        
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: Key_引导页已显示)
        
    }
    
    
    //MARK:UIScrollView代理
    
    func scrollViewDidScroll(scrollView: UIScrollView){
        
        if scrollView.contentOffset.x > (scrollView.contentSize.width - scrollView.bounds.width){
            从引导页切换到内容_函数()
        }
        
    }
    
    
    

}
